package cn.edu.nwsuaf.examcenter.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.nwsuaf.examcenter.pojo.ExamItemType;
import cn.edu.nwsuaf.examcenter.service.CourceEmployeeService;
import cn.edu.nwsuaf.examcenter.service.ExamContentService;
import cn.edu.nwsuaf.examcenter.service.ExamItemTypeService;
import cn.edu.nwsuaf.examcenter.service.ExamPaperService;
import cn.edu.nwsuaf.examcenter.service.StudentExamService;
import cn.edu.nwsuaf.examcenter.service.StudentService;

@Controller
@RequestMapping(value="/exam")
public class ExamController {
	@Resource(name="examItemTypeService")
	private ExamItemTypeService examItemTypeService;
	
	@Resource(name="examPaperService")
	private ExamPaperService examPaperService;
	
	@Resource(name="examContentService")
	private ExamContentService examContentService;
	
	@Resource(name="courceEmployeeService")
	private CourceEmployeeService courceEmployeeService;
	
	@Resource(name="studentService")
	private StudentService studentService;
	
	@Resource(name="studentExamService")
	private StudentExamService studentExamService;
	
	/*------------------题型处理-------------------------------------------*/
	@ResponseBody
	@RequestMapping(value="/addExamItemType",method=RequestMethod.POST)
	public String addExamItemType(@ModelAttribute ExamItemType examItemType){
		return examItemTypeService.addExamItemType(examItemType);
	}
	
	@ResponseBody
	@RequestMapping(value="/importExamItemType",method=RequestMethod.POST)
	public String importExamItemType(@ModelAttribute ExamItemType examItemType){
		return examItemTypeService.importExamItemType(examItemType);
	}
	
	@RequestMapping(value="/loadExamItemType")
	public String loadExamItemType(ModelMap modelMap){
		examItemTypeService.loadExamItemType(modelMap);
		return "exam/exam-item-type-list";
	}
	@ResponseBody
	@RequestMapping(value="/json/deleteExamItemType/{typeNo}")
	public String jsonDeleteExamItemType(@PathVariable("typeNo")String typeNo){
		return examItemTypeService.deleteExamItemType(typeNo);
	}
	@ResponseBody
	@RequestMapping(value="/json/deleteExamItemTypeList")
	public String jsonDeleteExamItemTypeList(@RequestParam(value="array")String array){
		return examItemTypeService.deleteExamItemTypeList(array);
	}
	/*-------------------题型处理-------------------------------------------*/
	
	/*-------------------试卷审核-------------------------------------------*/
	@RequestMapping(value="/loadExamPaperCheckList")
	public String loadExamPaperCheckList(@RequestParam(value="p",required=false)String p, ModelMap modelMap){
		examPaperService.loadExamPaperCheck(modelMap);
		return "exam/exam-paper-check-list";
	}
	
	/*试卷预览*/
	@RequestMapping(value="/examContentPreview/{examPaperNo}")
	public String examContentPreview(@PathVariable("examPaperNo")String examPaperNo, ModelMap modelMap){
		modelMap.addAttribute("examPaper",examPaperService.getExamPaperInfo(examPaperNo));
		examContentService.examContentPreview(examPaperNo, modelMap);
		return "exam/exam-content-preview";
	}
	
	@ResponseBody
	@RequestMapping(value="/passExamPaperCheck/{examPaperNo}")
	public String passExamPaperCheck(@PathVariable("examPaperNo")String examPaperNo){
		//第一步  修改exampaperInfo ischecked字段
		//第二步  根据课程字段来查询那些学生参加考试
		//第三步 把学生信息和考试信息绑定到student-exam-info表中。
			String courceNo = examPaperService.passExamPaperCheck(examPaperNo);//根据试卷编号获取课程编号
			List<String> classNoList = courceEmployeeService.getSclassNoByCourceNo(courceNo);//根据课程编号获取班级编号
			List<String> studentNoList = studentService.getStudentNoBySclassNo(classNoList);//根据编辑编号获取学生编号
			studentExamService.addStudentExam(examPaperNo, studentNoList);//将学生编号和试卷编号插入学生试卷表
		return "success";
	}
	@ResponseBody
	@RequestMapping(value="/failExamPaperCheck/{examPaperNo}")
	public String failExamPaperCheck(@PathVariable("examPaperNo")String examPaperNo){
		examPaperService.failExamPaperCheck(examPaperNo);
		return "success";
	}
	
	/*-------------------------------试卷信息查询------------------------------------*/
	@RequestMapping(value="loadExamPaper")
	public String loadExamPaper(@RequestParam(value="key", required=false)String key, @RequestParam(value="p",required=false)String p, ModelMap modelMap){
		examPaperService.loadExamPaper(modelMap);
		return "exam/exam-paper-list";
	}
}
